package com.example.myfirstgame.helper;

import java.util.ArrayList;
import java.util.HashMap;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;

import com.example.myfirstgame.resources.Colors;

public class Raster {

	private int mRasterSize;
	private ArrayList<RasterPoint> mRPList;
	private int[][] mLineArrayHeight = null;
	private int[][] mLineArrayWidth = null;
	private int[] mMaxHeightAndWidth = new int[2];
	private Paint mPaint = null;
	private HashMap<String, RasterPoint> mRasterMap = null;

	public Raster(int pRasterSize) {
		mRPList = new ArrayList<RasterPoint>();
		mRasterSize = pRasterSize;
	}

	public void addRasterPoint(RasterPoint pRasterPoint) {
		pRasterPoint.setRasterSize(mRasterSize);
		mRPList.add(pRasterPoint);
	}

	public ArrayList<RasterPoint> getRPList() {
		return mRPList;
	};

	public void draw(Canvas pCanvas) {
		if (mPaint == null) {
			mPaint = new Paint();
			mPaint.setColor(Colors.LIGHT_GREEN);
			mPaint.setStrokeWidth(1f);
		}
		if (mLineArrayHeight == null || mLineArrayWidth == null) {
			createDrawableRaster();
		}// [h_min,w_min,h_max,w_max]
		for (int[] pStartEndPoint : mLineArrayHeight) {
			pCanvas.drawLine(pStartEndPoint[1], pStartEndPoint[0],
					pStartEndPoint[3], pStartEndPoint[2], mPaint);
		}
		for (int[] pStartEndPoint : mLineArrayWidth) {
			pCanvas.drawLine(pStartEndPoint[1], pStartEndPoint[0],
					pStartEndPoint[3], pStartEndPoint[2], mPaint);
		}
	}

	private void createDrawableRaster() {
		int pMinWidth = 0;
		int pMinHeight = 0;
		int pMaxWidth = 0;
		int pMaxHeight = 0;
		for (RasterPoint lRasterPoint : mRPList) {
			int pActHeight = Integer.valueOf(lRasterPoint.getCellIDHeight());
			int pActWidth = Integer.valueOf(lRasterPoint.getCellIDWidth());
			if (pActHeight > pMaxHeight) {
				pMaxHeight = pActHeight;
			}
			if (pActWidth > pMaxWidth) {
				pMaxWidth = pActWidth;
			}
		}
		// [h_min,w_min,h_max,w_max]
		int[][] pLinesHeight = new int[pMaxHeight+1][4];
		int[][] pLinesWidth = new int[pMaxWidth+1][4];

		for (RasterPoint lRasterPoint : mRPList) {

			int pActHeight = Integer.valueOf(lRasterPoint.getCellIDHeight());
			int pActWidth = Integer.valueOf(lRasterPoint.getCellIDWidth());
			// Log.i("RASTER", "pActHeight: " + pActHeight + "; " + "pActWidth:"
			// + pActWidth);

			if (pActHeight == pMinHeight) {
				int pIdWidth = Integer.valueOf(lRasterPoint.getCellIDWidth());
				pLinesWidth[pIdWidth][0] = (int) lRasterPoint
						.getCoordinate().getY();
				pLinesWidth[pIdWidth][1] = (int) lRasterPoint
						.getCoordinate().getX();
			} else if (pActHeight == pMaxHeight) {
				int pIdWidth = Integer.valueOf(lRasterPoint.getCellIDWidth());
				pLinesWidth[pIdWidth][2] = (int) lRasterPoint
						.getCoordinate().getY();
				pLinesWidth[pIdWidth][3] = (int) lRasterPoint
						.getCoordinate().getX();
			}
			if (pActWidth == pMinWidth) {
				int pIdHeight = Integer.valueOf(lRasterPoint.getCellIDHeight());
				pLinesHeight[pIdHeight][0] = (int) lRasterPoint
						.getCoordinate().getY();
				pLinesHeight[pIdHeight][1] = (int) lRasterPoint
						.getCoordinate().getX();
			} else if (pActWidth == pMaxWidth) {
				int pIdHeight = Integer.valueOf(lRasterPoint.getCellIDHeight());
				pLinesHeight[pIdHeight][2] = (int) lRasterPoint
						.getCoordinate().getY();
				pLinesHeight[pIdHeight][3] = (int) lRasterPoint
						.getCoordinate().getX();
			}
		}
		mLineArrayHeight = pLinesHeight;
		mLineArrayWidth = pLinesWidth;
	}

	public int getmRasterSize() {
		return mRasterSize;
	}

	public void setmRasterSize(int mRasterSize) {
		this.mRasterSize = mRasterSize;
	}

	public void setmMaxHeightAndWidth(int[] mMaxHeightAndWidth) {
		this.mMaxHeightAndWidth = mMaxHeightAndWidth;
	}
	
	private HashMap<String, RasterPoint> createRasterPointHashMap() {
		
		HashMap<String, RasterPoint> pRasterMap = new HashMap<String, RasterPoint>();
		
		if (mRPList != null) {
			for (RasterPoint lRasterPoint : mRPList) {
				// index Height , Width
				int[] indexH_W = new int[2];
				indexH_W[0] = Integer.valueOf(lRasterPoint.getCellIDHeight());
				indexH_W[1] = Integer.valueOf(lRasterPoint.getCellIDWidth());
				String key = indexH_W[0] + ";" +indexH_W[1];
				pRasterMap.put(key, lRasterPoint);
			}
		}
		return pRasterMap;
	}

	public RasterPoint getRasterPoint(int pIndexHeight, int pIndexWidth) {

		if (mRasterMap == null) {
			mRasterMap = createRasterPointHashMap();
		}
		
		int[] indexH_W = new int[2];
		indexH_W[0] = pIndexHeight;
		indexH_W[1] = pIndexWidth;
		String key = indexH_W[0] + ";" +indexH_W[1];
		return mRasterMap.get(key);

	}

	public RasterPoint getRasterPoint(float pHeightInPercentage,
			float pWidthInPercentage) {

		if (pHeightInPercentage > 100f) {
			pHeightInPercentage = 100f;
		}
		if (pWidthInPercentage > 100f) {
			pWidthInPercentage = 100f;
		}

		int pIndexHeight = (int) (((float) mMaxHeightAndWidth[0]) * (pHeightInPercentage / 100));
		int pIndexWidth = (int) (((float) mMaxHeightAndWidth[1]) * (pWidthInPercentage / 100));

		return getRasterPoint(pIndexHeight, pIndexWidth);
	}
}
